package com.springboot.autoconfigure.oss.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import com.springboot.autoconfigure.oss.config.AliyunOSSPropertis;
import com.springboot.autoconfigure.oss.config.OssConfigPropertis;
import com.springboot.autoconfigure.oss.model.FileInfo;
import com.springboot.autoconfigure.oss.model.FileOssOperationStatus;
import com.springboot.autoconfigure.oss.model.FileUploadStatus;
import com.springboot.autoconfigure.oss.service.AbstractInitClientServer;
import com.springboot.autoconfigure.oss.service.OssApiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
public class AliyunOssApiServiceImpl implements OssApiService<AliyunOSSPropertis>, AbstractInitClientServer<OSS> {
    //文件名层级分隔符号
    private static final String SPLIT_PATH="_";

    //URL 上传指定文件名前缀
    private static final String URL_UPLOAD_PREFIX="UPLOAD-BY-URL";



    private AliyunOSSPropertis aliyunOSSPropertis;
    private OssConfigPropertis ossConfigPropertis;

    public AliyunOssApiServiceImpl(AliyunOSSPropertis aliyunOSSPropertis, OssConfigPropertis ossConfigPropertis) {
        this.aliyunOSSPropertis = aliyunOSSPropertis;
        this.ossConfigPropertis = ossConfigPropertis;
    }

    @Override
    public OSS getinit(){
       return new OSSClientBuilder().build(aliyunOSSPropertis.getEndpoint(), aliyunOSSPropertis.getAccessKeyId(), aliyunOSSPropertis.getAccessKeySecret());
    }

    /**
     * 生成文件名称
     * @param file
     * @return
     */
    public String getFileName(File file){
        String fileName=null;
        String extFileName=FileUtil.extName(file);
        if(StrUtil.isEmptyIfStr(extFileName)){
            fileName= FileUtil.mainName(file) + SPLIT_PATH + getFileDate() + SPLIT_PATH + UUID.randomUUID();
        }else {
            fileName = FileUtil.mainName(file) + SPLIT_PATH + getFileDate() + SPLIT_PATH + UUID.randomUUID()+"."+extFileName;
        }
        //指定的路径上传
        if (!StrUtil.isEmptyIfStr(ossConfigPropertis.getFolderPathName())){
            OSS getinit = getinit();
            fileName=ossConfigPropertis.getFolderPathName()+fileName;
        }
        return  fileName;
    }

    private String getFileDate() {
        return DatePattern.PURE_DATETIME_MS_FORMAT.format(new Date());
    }

    public String getFileName(URL url){
        return URL_UPLOAD_PREFIX+SPLIT_PATH + getFileDate() + SPLIT_PATH + UUID.randomUUID();
    }


    public BucketInfo getBucketInfo(){
        OSS oss = getinit();
       return oss.getBucketInfo(aliyunOSSPropertis.getBucketName());
    }
    @Override
    public FileUploadStatus uploadFile(URL url) {
        return null;
    }

    @Override
    public FileUploadStatus uploadFile(URL url,String filePrix){
        return null;
    }

    @Override
    public FileUploadStatus uploadFile(File file) {
        OSS oss = getinit();
        String fileName = getFileName(file);
        PutObjectRequest putObjectRequest = new PutObjectRequest(aliyunOSSPropertis.getBucketName(),fileName,file);
        oss.putObject(putObjectRequest);
        oss.shutdown();
        FileUploadStatus suceess = FileUploadStatus.suceess();
        suceess.setUrlIsPublic((getBucketInfo().getCannedACL()== CannedAccessControlList.PublicRead)||(getBucketInfo().getCannedACL()== CannedAccessControlList.PublicReadWrite));
        suceess.setFileName(fileName);
        if (suceess.isUrlIsPublic()) {
            suceess.setUrl(aliyunOSSPropertis.getExtranet() + File.separator + fileName);
        }else{

        }
        return suceess;
    }

   

    @Override
    public List<FileInfo> getBucketSpaceFile(String prefix, int limit) {
        return null;
    }

    @Override
    public FileUploadStatus uploadAbsoulteFilePath(File file, String filePath) {
        OSS oss = getinit();
        String fileName = getFileName(file);
        if (!StrUtil.isEmptyIfStr(filePath)){
            fileName=filePath+fileName;
        }
        PutObjectRequest putObjectRequest = new PutObjectRequest(aliyunOSSPropertis.getBucketName(),fileName,file);
        oss.putObject(putObjectRequest);
        oss.shutdown();
        FileUploadStatus suceess = FileUploadStatus.suceess();
        suceess.setUrlIsPublic((getBucketInfo().getCannedACL()== CannedAccessControlList.PublicRead)||(getBucketInfo().getCannedACL()== CannedAccessControlList.PublicReadWrite));
        suceess.setFileName(fileName);
        if (suceess.isUrlIsPublic()) {
            suceess.setUrl(aliyunOSSPropertis.getExtranet() + File.separator + fileName);
        }else{
            //todo
        }
        return suceess;
    }

    @Override
    public FileUploadStatus uploadFile(File file, String fileName) {
        return null;
    }

    @Override
    public FileUploadStatus uploadFileAndCallBack(File file, String callbackUrl) {
        return null;
    }

    @Override
    public List<String> findFile(String... fileNames) {
       List<String> resultUrl=new ArrayList<>();
        /* OSS oss = getinit();
        for (String fileName : fileNames) {
            GetObjectRequest getObjectRequest = new GetObjectRequest(aliyunOSSPropertis.getBucketName(),fileName);
            OSSObject object = oss.getObject(getObjectRequest);
        }
        oss.shutdown();
        return resultUrl;*/
        return resultUrl;
    }


    @Override
    public FileUploadStatus findFileByKey(String fileKey) {
        return null;
    }

    @Override
    public FileOssOperationStatus delFile(String... fileNames) {
        return null;
    }
}
